<template>
  <div class="individuation">
    <div class="panel panel1">
      <div class="title">
        授课风格
        <a style="float:right;font-size:12px;color:#00A2EA"
          href="javascript:;"
          @click="handlePre"
        >
          保存
        </a>
      </div>
      <div class="content">
        <div class="input-group">
          <div class="left-title">
            教师风格：
          </div>
          <div class="right-content">
            <el-checkbox-group v-model="checkList1">
              <el-checkbox :label="item.title" v-for="item,i in data.style" :key="i"></el-checkbox>
            </el-checkbox-group>
          </div>
        </div>
        <div class="input-group">
          <div class="left-title">
            擅教学生：
          </div>
          <div class="right-content">
            <el-checkbox-group v-model="checkList2">
              <el-checkbox :label="item.title" v-for="item,i in data.goodat" :key="i"></el-checkbox>
            </el-checkbox-group>
          </div>
        </div>
      </div>
    </div>
    <div class="panel panel2">
      <div class="title">
        工作经历
        <a style="float:right;font-size:12px;color:#00A2EA"
          href="javascript:;"
          @click="addNewJob"
        >
          新增
        </a>
      </div>
      <div class="content">
        <ul class="job-list">
          <li v-for="item,i in data.job" :key="i">
            <div class="input-group">
              <div class="left-title">
                工作时间：
              </div>
              <div class="right-content">
                <el-date-picker
                  v-model="item.start_time"
                  type="month"
                  placeholder="选择月">
                </el-date-picker>
                至
                <el-date-picker
                  v-model="item.end_time"
                  type="month"
                  placeholder="选择月">
                </el-date-picker>
              </div>
            </div>
            <div class="input-group">
              <div class="left-title">
                工作简介：
              </div>
              <div class="right-content">
                <el-input
                  type="textarea"
                  :rows="2"
                  placeholder="请输入内容"
                  v-model="item.intro">
                </el-input>
              </div>
            </div>
            <div class="option" style="text-align:right;">
              <a href="javascript:;" style="color:#FF3030" @click="deleteJob(item)">删除</a>
              <a href="javascript:;" style="color:#EF6401" @click="addNewJob" v-if="i === data.job.length - 1" >新增工作经历</a>
              <a href="javascript:;" style="color:#00A2EA" @click="preserve(item)">保存</a>
            </div>
          </li>
          <li v-if="newJob">
            <div class="input-group">
              <div class="left-title">
                工作时间：
              </div>
              <div class="right-content">
                <el-date-picker
                  v-model="newJob.start_time"
                  type="month"
                  placeholder="选择月">
                </el-date-picker>
                至
                <el-date-picker
                  v-model="newJob.end_time"
                  type="month"
                  placeholder="选择月">
                </el-date-picker>
              </div>
            </div>
            <div class="input-group">
              <div class="left-title">
                工作简介：
              </div>
              <div class="right-content">
                <el-input
                  type="textarea"
                  :rows="2"
                  placeholder="请输入内容"
                  v-model="newJob.intro">
                </el-input>
              </div>
            </div>
            <div style="text-align: right;">
              <a href="javascript:;" style="color:#FF3030" @click="deleteJob(newJob)">删除</a>
              <a href="javascript:;" style="color:#00A2EA" @click="preserve(newJob)">保存</a>
            </div>
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script>
import { getInfo, updateStyle, updateJob } from 'api/user'
import moment from 'moment'
export default {

  name: 'index',
  created() {
    getInfo()
    .then(res => {
      if (res.extra) {
        this.data = res.extra
        this.checkList1 = res.extra.style.filter(v => res.extra.profile.style.indexOf(v.id.toString()) !== -1).map(v => v.title)
        this.checkList2 = res.extra.goodat.filter(v => res.extra.profile.good_at.indexOf(v.id.toString()) !== -1).map(v => v.title)
      }
    })
    .catch(err => console.log(err))
  },
  data() {
    return {
      checkList1: [],
      checkList2: [],
      value1: '',
      value2: '',
      textarea: '',
      data: {},
      newJob: null
    };
  },
  methods: {
    fetchData() {
      getInfo()
      .then(res => {
        this.data = res.extra
        this.checkList1 = res.extra.style.filter(v => res.extra.profile.style.indexOf(v.id.toString()) !== -1).map(v => v.title)
        this.checkList2 = res.extra.goodat.filter(v => res.extra.profile.good_at.indexOf(v.id.toString()) !== -1).map(v => v.title)
      })
      .catch(err => console.log(err))
    },
    addNewJob() {
      if (!this.newJob) {
        this.newJob = {
          start_time: '',
          end_time: '',
          intro: ''
        }
      }
    },
    deleteJob(item) {
      if (item.id) {
        updateJob({ jobid: item.id }, 'get')
        .then(() => {
          this.newJob = null
          this.fetchData()
        })
        .catch(err => console.log(err))
      } else {
        this.newJob = null
      }
    },
    preserve(item) {
      updateJob({
        ...item,
        start_time: moment(item.start_time).format('YYYY-MM'),
        end_time: moment(item.end_time).format('YYYY-MM')
      }, 'post')
      .then(() => {
        this.newJob = null
        this.fetchData()
      })
      .catch(err => console.log(err))
    },
    handlePre() {
      const style = this.data.style.filter(v => this.checkList1.indexOf(v.title.toString()) !== -1).map(v => v.id).join(',')
      const goodat = this.data.goodat.filter(v => this.checkList2.indexOf(v.title.toString()) !== -1).map(v => v.id).join(',')
      updateStyle({ style, goodat })
      .then(() => {
        this.$message({
          message: '保存成功',
          type: 'success'
        })
      })
      .catch(err => console.log(err))
    }
  }
};
</script>

<style lang="scss" scoped>
.individuation{
  .panel{
    .title{
      background-color: #f4f4f4;
      padding: 0 20px;
      text-align: left;
      height: 40px;
      line-height: 40px;
    }
    .content{
      padding-bottom: 20px;
      .input-group{
        padding: 20px 0;
        width: 100%;
        overflow: hidden;
        border-bottom: 1px solid #e5e6e7;
        .left-title{
          width: 70px;
          text-align: right;
          float: left;
        }
        .right-content{
          width: 800px;
          float: left;
          .el-checkbox-group{
            .el-checkbox:first-child{
              margin-left: 15px;
            }
          }
        }
      }
    }
  }
  .panel2{
    .content{
      .job-list{
        >li{
          border-bottom: 1px solid #e5e6e7;
          .option{
            >a{
              margin: 0 5px;
            }
          }
          &:last-child{
            border: none;
          }
        }
      }
      .input-group{
        border: none;
        .left-title{
          line-height: 36px;
        }
        .right-content{

        }
      }
    }
  }
}
</style>